package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/permission")
@CrossOrigin
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 保存权限信息
     *
     * @param permission
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Permission permission) {
        try {
            permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
    }

    /**
     * 分页查询权限信息
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping
    public Result findPage(QueryPageBean queryPageBean) {
        //分页查询权限信息
        try {
            PageResult pageResult = permissionService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 根据id查询检查项详情
     *
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            Permission permission = permissionService.findById(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 更新权限信息
     *
     * @param permission
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Permission permission) {
        try {
            permissionService.update(permission);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }
    /**
     * 删除权限信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(path = "/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        try {
            permissionService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    /**
     * 查询所有权限信息
     *
     * @return
     */
    @GetMapping(path = "/all")
    public Result findAll() {
        try {
            List<Permission> permissions = permissionService.findAll();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 查询菜单关联的权限
     *
     * @return
     */
    @GetMapping(path = "/menu/association/{menuIds}")
    public Result findMenuAssociationPermission(@PathVariable("menuIds") Integer[] menuIds) {
        try {
            List<Permission> permissions = permissionService.findByMenuId(menuIds);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }


}
